﻿using Base.FileRemoteModel.Accredit;
using Base.FileRemoteModel.UpFile;
using Base.FileRemoteModel.UserFile;
using Base.FileRemoteModel.UserFile.Model;
using WeDonekRpc.Client.Attr;
using WeDonekRpc.Helper;

namespace Basic.FlowExtendService.lmpl
{
    [ClassLifetimeAttr(ClassLifetimeType.SingleInstance)]
    internal class FileService : IFileService
    {
        public bool Accredit ( long fileId, string accreditId, int validTime = 600 )
        {
            return new ApplyFileReadAccredit
            {
                AccreditId = accreditId,
                FileId = new long[] { fileId },
                ValidTime = validTime
            }.Send();
        }

        public void Drop ( long[] fileId )
        {
            new BatchDropFileById
            {
                Ids = fileId
            }.Send();
        }

        public UserFileData[] GetUserFiles ( string[] linkType, List<long> linkPkId )
        {
            return new GetUserFiles
            {
                LinkPk = linkPkId,
                LinkType = linkType
            }.Send();
        }

        public void Restore ( long[] fileId )
        {
            new RestoreUserFile
            {
                FileId = fileId
            }.Send();
        }

        public void Save ( long[] fileId, long linkBizPk )
        {
            new SaveFileList
            {
                FileId = fileId,
                LinkBizPk = linkBizPk
            }.Send();
        }

        public void Save ( long fileId, long linkBizPk )
        {
            new SaveFile
            {
                FileId = fileId,
                LinkBizPk = linkBizPk
            }.Send();
        }

        public void Save ( long fileId, long linkBizPk, string src )
        {
            if ( src.IsNotNull() )
            {
                long id = long.Parse(Path.GetFileNameWithoutExtension(src));
                if ( id == fileId )
                {
                    return;
                }
                this._Save(fileId, linkBizPk, id);
            }
            else
            {
                this.Save(fileId, linkBizPk);
            }
        }
        private void _Save ( long fileId, long linkBizPk, long dropId )
        {
            new SaveFile
            {
                FileId = fileId,
                LinkBizPk = linkBizPk,
                DropFileId = new long[]
                {
                   dropId
                }
            }.Send();
        }
        public void ToVoid ( long[] fileId )
        {
            new ToVoidUserFile
            {
                FileId = fileId
            }.Send();
        }
    }
}
